package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class SysActiviteProvider {

	private static SysActiviteProvider instance = null;

	public static SysActiviteProvider getInstance() {
		if (instance == null) {
			new SysActiviteProvider();
		}
		return instance;
	}

	// 定义系统表的name与操作语句
	private static final String table = "lz_sys_activite";
	private static final String key = "sys";

	private static final String INSERT_SYS_ACTIVITE = "REPLACE INTO " + table
			+ " (name,activite) " + "VALUES (?,?)";
	private static final String LOAD_SYS_ACTIVITE = "SELECT * FROM " + table
			+ "  WHERE (name=?)  ";
	private static final String DELETE_SYS_ACTIVITE = "DELETE  FROM " + table
			+ "  WHERE (name=?)  ";

	// 初始化
	private SysActiviteProvider() {
		instance = this;
	}

	// 取系统平台促销信息
	public String load_SYSActivite() {
		String tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(LOAD_SYS_ACTIVITE);
			pstmt.setString(1, key);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = rs.getString(2) + "";// 店铺id
			}

		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}

	// 更新系统平台促销信息
	public String update_SYSActivite(String activite) {
		Connection con = null;
		PreparedStatement pstmt = null;
		// 生成ID号
		String tmp = null;
		try {
			// 得到时间
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(INSERT_SYS_ACTIVITE);
			if (CacheClass.isEmpty(activite))
				pstmt.setNull(1, Types.VARCHAR);
			else
				pstmt.setString(1, activite);
			pstmt.setString(2, key);

			boolean falg = pstmt.executeUpdate() == 1;
			if (falg) {
				tmp = activite;
			}
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(pstmt, con);
		}
		return tmp;
	}

	// 清空系统平台促销信息
	public boolean delete_SYSActivite() {
		boolean success = false;
		Connection con = null;
		PreparedStatement pstmt = null;
		// 生成ID号
		String tmp = load_SYSActivite();
		// 如果没有，则新建店q记录
		if (!CacheClass.isEmpty(tmp)) {
			try {
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement(DELETE_SYS_ACTIVITE);
				pstmt.setString(1, key);
				boolean falg = pstmt.executeUpdate() == 1;
				if (falg) {
					success = true;
				}

			} catch (Exception e) {
				success = false;
				e.printStackTrace();
			} finally {
				DbConnectionManager.closeConnection(pstmt, con);
			}
		}
		tmp = null;
		return success;
	}

}
